package auth

import (
	"errors"
	"net/http"

	"gitee.com/libwiki/go-zero-lib/package/response"

	"gitee.com/libwiki/go-zero-lib/app/order/internal/logic/auth"
	"gitee.com/libwiki/go-zero-lib/app/order/internal/svc"
	"gitee.com/libwiki/go-zero-lib/app/order/internal/types"
	"github.com/zeromicro/go-zero/rest/httpx"
)

func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// 错误拦截
		defer func() {
			if result := recover(); result != nil {
				errMessage, ok := result.(string)
				//errMessage := fmt.Sprintf("%v", result)
				if ok { // 如果是字符串则直接返回结果
					response.Response(w, nil, errors.New(errMessage))
				} else {
					panic(result)
				}

			}
		}()
		var req types.LoginRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}

		l := auth.NewLoginLogic(r.Context(), svcCtx)

		resp, err := l.Login(&req)

		response.Response(w, resp, err)

	}
}
